﻿<template>
	<el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible"
		class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class="">
			<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
				<el-col :span="24" v-if="!this.dataForm.id">
					<el-form-item label="类型" prop="type">
						<el-radio-group v-model='optionsType' size='small' @change="dataForm.tenantId = ''">
							<el-radio-button v-for="(item, index) in options1" :key="index" :label="item.id">{{
								item.fullName }}</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="optionsType == '2' && !this.dataForm.id">
					<el-form-item label="租户" prop="tenantId">
						<el-select v-model='dataForm.tenantId' placeholder='请选择' clearable :style='{ "width": "100%" }'>
							<el-option v-for="(item, index) in tenantData" :key="index" :label="item.fullName"
								:value="item.id"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item :label="optionsType == '1' ? '租户名称' : '系统名称'" prop="fullName">
						<el-input v-model='dataForm.fullName' placeholder='请输入' clearable required
							:style='{ "width": "100%" }'>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="IP地址" prop="ipAddress">
						<el-input v-model='dataForm.ipAddress' placeholder='请输入' clearable required
							:style='{ "width": "100%" }'>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="端口号" prop="port">
						<el-input v-model='dataForm.port' placeholder='请输入' clearable :style='{ "width": "100%" }'>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="有效标志" prop="enabledMark">
						<el-switch v-model='dataForm.enabledMark' :active-value='1' :inactive-value='0'>
						</el-switch>
					</el-form-item>
				</el-col>
			</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { getTenantInfo, getTenantAll, createTenant, updateTenant, getTenantExternalInfo, createTenantExternal, updateTenantExternal } from '@/api/system/interface/tenant'
export default {
	components: {},
	props: [],
	data() {
		const tenantIdValidator = (rule, value, callback) => {
			console.log(value)
			if (value.length == 0 && this.optionsType=="2") {
				callback(new Error("请选择租户"));
			} else {
				callback();
			}
		};
		return {
			btnLoading: false,
			loading: false,
			visible: false,
			optionsType: "1",
			dataForm: {
				id: '',
				tenantId: "",
				fullName: undefined,
				ipAddress: undefined,
				port: undefined,
				enabledMark: 1,
			},
			rules: {
				tenantId: [
					{
						required: true,
						validator: tenantIdValidator,
						trigger: '"blur"'
					},
				],
				fullName: [
					{
						required: true,
						message: '请输入租户名称',
						trigger: '"blur"'
					},
				],
				ipAddress: [
					{
						required: true,
						message: '请输入IP地址',
						trigger: '"blur"'
					},
				],
			},
			options1: [{ 'fullName': "租户", 'id': "1" }, { 'fullName': "外部系统", 'id': "2" }],
			tenantData: [],
		}
	},
	computed: {},
	watch: {},
	created() {
	},
	mounted() {
	},
	methods: {
		goBack() {
			this.$emit('refresh')
		},
		init(id, parentId) {
			this.dataForm.id = id || 0;
			this.optionsType = parentId == "-1" ? "1" : "2";
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['elForm'].resetFields();
				getTenantAll().then(res => {
					this.tenantData = res.data.list;
				})

				if (this.dataForm.id) {
					if (this.optionsType == "1") {
						getTenantInfo(this.dataForm.id).then(res => {
							this.dataForm = res.data;
						})
					} else {
						getTenantExternalInfo(this.dataForm.id).then(res => {
							this.dataForm = res.data;
						})
					}
				}
			});
		},
		dataFormSubmit() {
			this.$refs['elForm'].validate((valid) => {
				if (valid) {
					this.btnLoading = true;
					let formMethod = !this.dataForm.id ? createTenant : updateTenant;
					if (this.optionsType == '2') {
						formMethod = !this.dataForm.id ? createTenantExternal : updateTenantExternal;
					}
					formMethod(this.dataForm).then((res) => {
						this.btnLoading = false;
						this.$message({
							message: res.msg,
							type: 'success',
							duration: 1000,
							onClose: () => {
								this.visible = false,
									this.$emit('refresh', true)
							}
						})
					}).catch((res) => {
						this.btnLoading = false;
					})
				}
			})
		},
	}
}
</script>
